/*
 * @Author: Turump 
 * @Date: 2021-03-29 17:05:29 
 * @Last Modified by: Turump
 * @Last Modified time: 2021-09-17 11:23:54
 */

import ActionHelper from "../helper/ActionHelper";
import CocosHelper, { Direction } from "../helper/CocosHelper";
import { TaskBase } from "./TaskBase";

const { ccclass, property, menu } = cc._decorator;


enum MoveActionType {
    Bezier = 1,
    EaseElastic,
    JumpTo,
    Rotate
}

@ccclass
@menu("任务组件/进场")
export default class MoveInTask extends TaskBase {
    @property({ displayName: "动作时长(s)" })
    actionDuration = 0.75

    @property({ type: cc.Enum(MoveActionType), displayName: "进场动作类型" })
    actionType: MoveActionType = MoveActionType.EaseElastic

    @property({ displayName: "进场方向", type: cc.Enum(Direction) })
    moveInDirection: Direction = Direction.Left

    @property({displayName:"自定义音效"})
    isSelfAudio = false;
    
    @property({ displayName: "自定义进场音效", type: cc.AudioClip, visible(){return this.isSelfAudio} })
    moveInAudioClip: cc.AudioClip = null;

    //进场开始及目标坐标
    startPostion: cc.Vec2 = cc.Vec2.ZERO
    endPosition: cc.Vec2 = cc.Vec2.ZERO

    onLoad() {
        super.onLoad();
    }

    start() {
        super.start();
        this.node.opacity = 0;
    }

    enableTask() {
        super.enableTask();
        this.endPosition = this.node.getPosition()
        this.node.setPosition(CocosHelper.getNodeOutScreenPosition(this.node, this.moveInDirection));
        this.startPostion = this.node.getPosition()
        this.node.opacity = 255;
        this.node.runAction(this.getAction())
        cc.log(`${this.name} Move in start:${this.startPostion.toString()}, end:${this.endPosition.toString()}`)
    }

    private getAction() {
        let action;
        switch (this.actionType) {
            case MoveActionType.Bezier:
                action = ActionHelper.getBezierAction(this.actionDuration, this.startPostion, this.endPosition);
                break
            case MoveActionType.JumpTo:
                action = cc.jumpTo(this.actionDuration, this.endPosition, 200, 1);
                break;
            case MoveActionType.Rotate:
                let r = this.moveInDirection == Direction.Left ? 10 : -10;
                action = cc.spawn(cc.moveTo(this.actionDuration, this.endPosition).easing(cc.easeBackOut()),
                    cc.sequence(cc.rotateBy(this.actionDuration * .58, r), cc.rotateBy(this.actionDuration * 0.58, -r).easing(cc.easeBackInOut())))
                break;
            default:
                action = cc.moveTo(this.actionDuration, this.endPosition).easing(cc.easeElasticOut(2.0));
        }
        let doneCall = cc.callFunc(() => {
            this.emitDoneEvent();
        })
        let audioCall = cc.callFunc(() => {
            if (this.isSelfAudio && this.moveInAudioClip) {
                cc.audioEngine.playEffect(this.moveInAudioClip, false);
            } else {
            }
        })
        return cc.sequence(audioCall, action, doneCall)
    }
}